﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_CauHoi_DanhSachCauHoi : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
            LoadMess();
        }
    }

    private void LoadMess()
    {
        if (Session["Mess"] != null && Session["Type"].ToString().Equals("Success"))
        {
            String mess = "<div class='alert alert-success alert-dismissable'>";
            mess += "<i class='fa fa-check'></i>";
            mess += "<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>";
            mess += Session["Mess"].ToString() + "</div>";
            Session.Remove("Mess");
            Session.Remove("Type");
            ltrMess.Text = mess;
        }
        if (Session["Mess"] != null && Session["Type"].ToString().Equals("Error"))
        {
            String mess = "<div class='alert alert-danger alert-dismissable'>";
            mess += "<i class='fa fa-ban'></i>";
            mess += "<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>";
            mess += Session["Mess"].ToString() + "</div>";
            Session.Remove("Mess");
            Session.Remove("Type");
            ltrMess.Text = mess;
        }
    }

    private void LoadData()
    {
        DataTable ds = CauHoiDAL.danhsach();
        GridView1.DataSource = ds;
        GridView1.DataBind();
       
        List<LoaiCauHoiINFO> dsloaicauhoi = LoaiCauHoiDAL.listAll();
        ddltenloai.DataSource = dsloaicauhoi;
        ddltenloai.DataTextField = "TenLoai";
        ddltenloai.DataValueField = "MaLoai";
        ddltenloai.DataBind();
        ddltenloai.Items.Insert(0, new ListItem("Chọn loại câu hỏi", "0"));

        List<MonThiINFO> dsmonthi = MonThiDAL.listAll();
        ddltenmon.DataSource = dsmonthi;
        ddltenmon.DataTextField = "TenMon";
        ddltenmon.DataValueField = "MaMon";
        ddltenmon.DataBind();
        ddltenmon.Items.Insert(0, new ListItem("Chọn môn thi", "0"));

        List<DoKhoINFO> dsdokho = DoKhoDAL.listAll();
        ddldokho.DataSource = dsdokho;
        ddldokho.DataTextField = "DoKho";
        ddldokho.DataValueField = "MaDoKho";
        ddldokho.DataBind();
        ddldokho.Items.Insert(0, new ListItem("Tất cả", "0"));
    
    }
    protected void ddltenloai_SelectedIndexChanged(object sender, EventArgs e)
    {
        search();
    }
    protected void ddltenmon_SelectedIndexChanged(object sender, EventArgs e)
    {
        search();
    }

    public void search() {
        int maloai = int.Parse(ddltenloai.SelectedValue.ToString());
        int mamon = int.Parse(ddltenmon.SelectedValue.ToString());
        int dokho = int.Parse(ddldokho.SelectedValue.ToString());
        DataTable ds = CauHoiDAL.timkiem(maloai, mamon, dokho);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void ddldokho_SelectedIndexChanged(object sender, EventArgs e)
    {
        search();
    }
}